package team.seekers.catering.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.entity.dataobject.Ingredient;
import team.seekers.catering.entity.params.add.FoodUpdatePA;
import team.seekers.catering.entity.params.query.IngredientQuery;
import team.seekers.catering.entity.params.update.IngredientUpdatePA;
import team.seekers.catering.entity.vo.info.FoodInfoVO;
import team.seekers.catering.entity.vo.info.IngredientInfoVO;
import team.seekers.catering.service.IngredientService;

import java.util.Map;

/**
 * <p>
 * 食材表 前端控制器
 * </p>
 *
 * @author hbc
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/ingredient")
public class IngredientController {

    @Autowired
    private IngredientService ingredientService;

    /**
     * 查询食材
     */
    @PostMapping("/adminPageOrder/{current}/{limit}")
    public R adminPageOrder(@PathVariable("current") long current,
                            @PathVariable("limit") long limit,
                            @RequestBody IngredientQuery query){
        QueryWrapper<Ingredient> wrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(query.getNameKey())){
            wrapper.like("nc_name",query.getNameKey());
        }
        Map<String, Object> map = ingredientService.adminPageOrder(current,limit,wrapper);
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }


    /**
     * 食材详情
     */
    @GetMapping("/query/{ingredientId}")
    public R query(@PathVariable("ingredientId")String id){
        IngredientInfoVO ingredientInfoVO = ingredientService.get(id);
        return R.success().data("info",ingredientInfoVO);
    }

    /**
     * 修改食材
     */
    @PostMapping("/update")
    public R update(@RequestBody IngredientUpdatePA ingredientUpdatePA){
        Long id = ingredientService.update(ingredientUpdatePA);
        return R.success().data("id",id);
    }


    /**
     * 删除食材
     */
    @GetMapping("/delete/{ingredientId}")
    public R delete(@PathVariable("ingredientId") Long ingredientId){
        ingredientService.removeById(ingredientId);
        return R.success();
    }
}

